package it.fantacalcio.struts.action.amministrazione.user;

import it.fantacalcio.model.Role;
import it.fantacalcio.model.User;
import it.fantacalcio.spring.service.MyServiceFactory;
import it.fantacalcio.struts.form.UserForm;
import it.fantacalcio.utility.Utility;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class ConfermaCRUDUserAction extends DispatchAction {
	
	@Override
	protected ActionForward dispatchMethod(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3, String arg4)
			throws Exception {
		
		User userInSession = (User) arg2.getSession().getAttribute("userInSession");
		if(userInSession==null || userInSession.getAttivo().equals("NO") || !userInSession.getRole().getCodRole().equals("ADMIN")){
			return arg0.findForward("noninsession");
		}
		return super.dispatchMethod(arg0, arg1, arg2, arg3, arg4);
	}
	
	public ActionForward aggiungi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		UserForm userForm = (UserForm) form;
		List<User> listaUser = new ArrayList<User>();
		try {
			User user = new User();
			BeanUtils.copyProperties(user, userForm);
			user.setRole(MyServiceFactory.getRoleService().get(userForm.getIdRole()));
			int ris = MyServiceFactory.getUserService().salva(user);
			if(ris < 1){
				List<Role> listaRole = MyServiceFactory.getRoleService().list();
				request.setAttribute("listaRole", listaRole);
				request.setAttribute("msg", "User non aggiunto");
				return mapping.findForward("aggiungi");
			}
			listaUser = MyServiceFactory.getUserService().list();
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaUser", listaUser);
		request.setAttribute("msg", "User aggiunto con successo");
		return mapping.findForward("ok");
	}
	
	public ActionForward modifica(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		UserForm userForm = (UserForm) form;
		List<User> listaUser = new ArrayList<User>();
		try {
			User user = MyServiceFactory.getUserService().get(userForm.getId());
			if(!userForm.getPassword().equals(user.getPassword())){
				userForm.setPassword(Utility.md5hash(userForm.getPassword()));
			}
			BeanUtils.copyProperties(user, userForm);
			user.setRole(MyServiceFactory.getRoleService().get(userForm.getIdRole()));
			int risp = MyServiceFactory.getUserService().modifica(user);
			if(risp < 1){
				List<Role> listaRole = MyServiceFactory.getRoleService().list();
				request.setAttribute("listaRole", listaRole);
				request.setAttribute("msg", "User non modificato");
				return mapping.findForward("modifica");
			}
			listaUser = MyServiceFactory.getUserService().list();
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaUser", listaUser);
		request.setAttribute("msg", "User modificato con successo");
		return mapping.findForward("ok");
	}
	
	public ActionForward cancella(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		UserForm userForm = (UserForm) form;
		List<User> listaUser = new ArrayList<User>();
		try {
			User user = MyServiceFactory.getUserService().get(userForm.getId());
			user.setAttivo("NO");
			int risp = MyServiceFactory.getUserService().modifica(user);
			if(risp < 1){
				List<Role> listaRole = MyServiceFactory.getRoleService().list();
				request.setAttribute("listaRole", listaRole);
				request.setAttribute("msg", "User non cancellato");
				return mapping.findForward("cancella");
			}
			listaUser = MyServiceFactory.getUserService().list();
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaUser", listaUser);
		request.setAttribute("msg", "User cancellato con successo");
		return mapping.findForward("ok");
	}
	
}
